package com.pfzy.rxbus2demo

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

/**
 * RxBus管理
 */
class RxBusManagerActivity : AppCompatActivity() {
    private var tvSticky: TextView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_manager)
        tvSticky = findViewById(R.id.tv_about_manager)
        RxBusManager.subscribeRxBusManagerActivity(this)
    }

    /**
     * 文字改变
     */
    fun updateText(s: String?) {
        tvSticky?.text = s?.let { Config.appendMsg(it) }
    }

    override fun onDestroy() {
        super.onDestroy()
        RxBusManager.unregisterRxBusManagerActivity(this)
    }

    fun postWithoutTag(view: View?) {
        Config.restoreMsg()
        RxBusManager.postToRxBusManagerActivity("tag")
    }

    fun postWithTag(view: View?) {
        Config.restoreMsg()
        RxBusManager.postWithMyTagToRxBusManagerActivity("tag")
    }

    fun postStickyWithoutTag(view: View?) {
        Config.restoreMsg()
        RxBusManager.postStickyToRxBusManagerActivity("tag")
        StickyTestActivity.start(this)
    }

    fun postStickyWithTag(view: View?) {
        Config.restoreMsg()
        RxBusManager.postStickyWithMyTagToRxBusManagerActivity("tag")
        StickyTestActivity.start(this)
    }

    companion object {
        fun start(context: Context) {
            val starter = Intent(context, RxBusManagerActivity::class.java)
            context.startActivity(starter)
        }
    }
}
